package com.lightningtrade.security;

import ch.qos.logback.core.joran.spi.NoAutoStart;
import com.lightningtrade.domain.SysUser;
import jdk.jfr.DataAmount;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;


public class UserDetails implements org.springframework.security.core.userdetails.UserDetails {

    private Integer uid;
    private String username;
    private String password;

    private List<String> permissions;

    private transient List<GrantedAuthority> authorities;

    public UserDetails(SysUser user) {
        this.uid = user.getUid();
        this.username = user.getUsername();
        this.password = user.getPassword();
        this.permissions = user.getRole();
    }


    public Integer getUid() {
        return uid;
    }

    // 获取权限信息
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if(authorities != null) {
            return authorities;
        }
        // 将权限信息添加到GrantedAuthority集合中
        authorities = permissions.stream()
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());
//        return null;
        return authorities;
    }

    @Override
    public String getPassword() { //获取密码

        return password;
    }
    @Override
    public String getUsername() { //获取用户名
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() { //认证期限状态
        return true;
    }

    @Override
    public boolean isEnabled() { //用户是否可用
        return true;
    }
}
